from building import *

Import('BUILD_DIR')
CWD = GetCurrentDir()

@register_os
class LibraryFreeRTOS(Library):
    def get_objs(self, pkg):
        if IsBuildForHost(self.GetCompiler()):
            objs = ['*.c', 'portable/MemMang/heap_3.c']
            if IsBuildForWindows():
                objs += ['portable/MSVC-MingW/*.c']
            else:
                objs += ['portable/ThirdParty/GCC/Posix/*.c',
                        'portable/ThirdParty/GCC/Posix/utils/*.c']
        else:
            arch = self.GetArch()
            objs = ['*.c', 'portable/MemMang/heap_5.c']
            objs += ['portable/%s/*.c'%(arch)]
            self.include += ['%s/portable/%s' % (pkg, arch)]
        return PkgGlob(pkg, objs)

    def config(self):
        variant = self.GetOSVariant()
        if None != variant:
            self.LIBS = [variant]
            self.source = []
            return
        pkg = Package(
            url='https://github.com/FreeRTOS/FreeRTOS-Kernel.git', version='V10.4.6')
        self.source = self.get_objs(pkg)
        self.include += ['%s/include' % (pkg), '%s/config' % (CWD)]

        if IsBuildForHost(self.GetCompiler()):
            if IsBuildForWindows():
                self.include += ['%s/portable/MSVC-MingW' % (pkg)]
                self.LIBS += ['winmm']
            else:
                self.include += [
                    '%s/portable/ThirdParty/GCC/Posix' % (pkg),
                    '%s/portable/ThirdParty/GCC/Posix/utils' % (pkg)]
